﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint;
using System.Collections.Specialized;

namespace Slam.Logging
{	
	public static class ExceptionEmailer
	{
		private static string _emailFormat = @"
		<html>
			<head>
				<title>{0}</title>
				<style>
				 body {font-family:""Verdana"";font-weight:normal;font-size: .7em;color:black;} 
				 p {font-family:""Verdana"";font-weight:normal;color:black;margin-top: -5px}
				 b {font-family:""Verdana"";font-weight:bold;color:black;margin-top: -5px}
				 H1 { font-family:""Verdana"";font-weight:normal;font-size:18pt;color:red }
				 H2 { font-family:""Verdana"";font-weight:normal;font-size:14pt;color:maroon }
				 pre {font-family:""Lucida Console"";font-size: .9em}
				 .marker {font-weight: bold; color: black;text-decoration: none;}
				 .version {color: gray;}
				 .error {margin-bottom: 10px;}
				 .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
				</style>
			</head>

			<body bgcolor=""white"">
					<h2> <i>{0}</i> </h2></span>

					<font face=""Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "">
					<table width=100% bgcolor=""#ffffcc"">
					   <tr>
						  <td>
							  <code><pre>
		{1}
		</pre></code>

						  </td>
					   </tr>
					</table>

					<br>

					<hr width=100% size=1 color=silver>

					<b>Version Information:</b>&nbsp;Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

					</font>

			</body>
		</html>";

		public static void Send(SPWeb site, string toAddress, Exception ex)
		{
			StringDictionary headers = new StringDictionary();
			headers.Add("to", toAddress);
			headers.Add("subject", "Error at: " + site.Url);
			headers.Add("content-type", "text/html");

			SPUtility.SendEmail(site, headers, String.Format(_emailFormat, ex.Message, ex.ToString()));
		}
	}
}
